/*************************************************
 *FileName:      handlersharp.js
 *Description:   经理人管理.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var page = 1,page_num=6;
    var isUp = true;
    /*
     * 主程序
     */ 
     
    window.addEventListener('handlersharp', function(e){//执行刷新
        lodemsg(); 
    });
    window.addEventListener('refreshindex', function(e){//执行刷新
        lodemsg(); 
    });
    $(document).ready(function() 
    {
        pagecss();
        //获取信息
//      lodemsg();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(lodemsg)
        $('.handlersharp .mui-content').on('tap','.title',function(){
            $(this).find('.mui-icon').toggleClass('mui-icon-arrowdown').toggleClass('mui-icon-arrowup');
            $(this).parent().find('.newfriend-text').toggle();
        })
        
        $('.handlersharp').on('tap','.agree',function(){
            var dataid = $(this).parents('.fold-text').attr('data-id');
            common.pageTransition('dataattestation.html?that=look&&dataid='+dataid)
        })
        
        $('.handlersharp').on('tap','.look',function(){
            var dataid = $(this).parents('.fold-text').attr('data-id');
            common.pageTransition('handler.html?that=look&&dataid='+dataid)
        })
        
        $('.handlersharp').on('tap','.untie',function(){
            if($(this).parents('.fold-text').hasClass('pt')){
                $('.table-view-cell-a1').css({display:'none'})
            }else{
                $('.table-view-cell-a1').css({display:'block'})
            }
            $('.table-view3').fadeIn();
            $('.popover').css({display :'block'});
        })
        
        $('.handlersharp').on('tap','.fold-text img',function(){
            if($(this).parents('.fold-text').hasClass('pt')){
                return false;
            }
            var sid = $(this).parents('.fold-text').attr('data-uid');
            common.pageTransition('personal.html?friend_uid='+sid);
        })
        
        $('.table-view3').on('tap','.table-view-cell-a2',function(){
            $('.table-view3').fadeOut();
            $('.table-view1').fadeIn();
        })
        
        $('.table-view3').on('tap','.table-view-cell-a1',function(){
            var oid =$('.myprop').find('.fold-text').attr('data-id');
            updelet(oid,'');
        })
        
        $('.table-view1').on('tap','.table-view-cell-affirm',function(){
            var otext = $('.table-view-cell-input').val();
            otext==''? mui.toast('请填写解绑理由'): updelet(0,otext);
        })
        
        $('.handlersharp').on('tap','.table-view-cell-cancel',function(){
            $('.table-view').fadeOut();
            $('.popover').css({display :'none'});
        })
        
        $('.handlersharp').on('tap','.con-title a',function(){
            $(this).addClass('active').siblings('.link-a').removeClass('active');
            $('.mui-scroll-wrapper').eq($(this).index()).css({display:'block'}).siblings('.mui-scroll-wrapper').css({display:'none'});
        })
    }
    
    //获取信息
    function lodemsg() {
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/myUsers",
            dataType: 'jsonp',
            data: {uid: uid,token: token},
            success: function(data) {
                if(data.error_code == 0) {
                    $('.con-title .title i').text(data.data.invite_code);
                    $('.con-title .title .color').text(data.data.manager_income);
                    //我的用户
                    if(data.data.date_verify_apply.length>0){
                        var odate_verify_apply='';
                        for(var i=0 ; i<data.data.date_verify_apply.length ; i++ ){
                            var osex = data.data.date_verify_apply[i].sex== "woman" ? 'icon_girl' :'icon_boy';
                            odate_verify_apply+='<div class="fold-text" data-uid="'+data.data.date_verify_apply[i].uid+'" data-id="'+data.data.date_verify_apply[i].id+'"><img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.date_verify_apply[i].face+'" />'
                                            +'<div class="box"><div class="user">'+data.data.date_verify_apply[i].nickname+'</div>'
                                            +'<div class="age"><img src="images/'+osex+'.png"/><span>'+data.data.date_verify_apply[i].age+'岁</span></div></div>'
                                            +'<div class="text"><a class="agree" href="javascript:">查看</a><a class="disagree" href="javascript:">忽略</a></div></div>';
                        }
                        $('.appnew').css({display:'block'});
                        $('.appnew .title .color').text(data.data.date_verify_apply.length);
                        $('.appnew .newfriend-text').html(odate_verify_apply);
                    }else{
                        $('.appnew').css({display:'none'});
                    }
                    if(data.data.date_unbund_list.length>0){
                        var odate_unbund_list='';
                        for(var i=0 ; i<data.data.date_unbund_list.length ; i++ ){
                            var osex = data.data.date_unbund_list[i].sex== "woman" ? 'icon_girl' :'icon_boy';
                            odate_unbund_list+='<div class="fold-text" data-uid="'+data.data.date_unbund_list[i].uid+'" data-id="'+data.data.date_unbund_list[i].id+'"><img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.date_unbund_list[i].face+'" />'
                                            +'<div class="box"><div class="user">'+data.data.date_unbund_list[i].nickname+'</div>'
                                            +'<div class="age"><img src="images/'+osex+'.png"/><span>'+data.data.date_unbund_list[i].age+'岁</span></div></div>'
                                            +'<div class="text"><a class="untieagree" href="javascript:">同意</a>'
                                            +'<a class="disagree" href="javascript:">忽略</a></div></div>';
                        }
                        $('.date-unbund-list').css({display:'block'});
                        $('.date-unbund-list .title .color').text(data.data.date_unbund_list.length);
                        $('.date-unbund-list .newfriend-text').html(odate_unbund_list);
                    }else{
                        $('.date-unbund-list').css({display:'none'});
                    }
                    var ousers_list='';
                    for(var i = 0 ; i<data.data.users_list.length ; i++){
                        var osex = data.data.users_list[i].sex == "woman" ? 'icon_girl' :'icon_boy';
                        ousers_list+='<div class="con" ><div class="fold-text" data-uid="'+data.data.users_list[i].uid+'" friend_uid="'+data.data.users_list[i].uid+'"><img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.users_list[i].face+'" />'
                                    +'<div class="box"><div class="user">'+data.data.users_list[i].nickname+'</div>'
                                    +'<div class="age"><img src="images/'+osex+'.png"><span>'+data.data.users_list[i].age+'岁</span>'
                                    +'</div></div><a class="lia" href="javascript:">解绑</a></div></div>';
                    }
                    $('.users_list .mui-scroll .mydown').html(ousers_list);
                    
                    //我的经理人团队
                    if(data.data.manager_apply_list.length>0){//新的申请
                        var omanager_apply_list='';
                        for(var i=0 ; i<data.data.manager_apply_list.length ; i++ ){
                            var osex = data.data.manager_apply_list[i].sex== "woman" ? 'icon_girl' :'icon_boy';
                            omanager_apply_list+='<div class="fold-text" data-uid="'+data.data.manager_apply_list[i].uid+'"  data-id="'+data.data.manager_apply_list[i].id+'"><img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.manager_apply_list[i].face+'"/>'
                                            +'<div class="box"><div class="user">'+data.data.manager_apply_list[i].nickname+'</div>'
                                            +'<div class="age"><img src="images/'+osex+'.png"/><span>'+data.data.manager_apply_list[i].age+'岁</span></div></div>'
                                            +'<div class="text"><a class="look" href="javascript:">查看</a>'
                                            +'<a class="disagree" href="javascript:">忽略</a></div></div>';
                        }
                        $('.new-team').css({display:'block'});
                        $('.new-team .title .color').text(data.data.manager_apply_list.length);
                        $('.new-team .newfriend-text').html(omanager_apply_list);
                    }else{
                        $('.new-team').css({display:'none'});
                    }
                    if(data.data.unbund_list.length>0){//解绑
                        var ounbund_list='';
                        for(var i=0 ; i<data.data.unbund_list.length ; i++ ){
                            var osex = data.data.unbund_list[i].sex== "woman" ? 'icon_girl' :'icon_boy';
                            ounbund_list+='<div class="fold-text" data-uid="'+data.data.unbund_list[i].uid+'" data-id="'+data.data.unbund_list[i].id+'"><img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.unbund_list[i].face+'"/>'
                                            +'<div class="box"><div class="user">'+data.data.unbund_list[i].nickname+'</div>'
                                            +'<div class="age"><img src="images/'+osex+'.png"/><span>'+data.data.unbund_list[i].age+'岁</span></div></div>'
                                            +'<div class="text"><a class="untieagree" href="javascript:">同意</a>'
                                            +'<a class="disagree" href="javascript:">忽略</a></div></div>';
                        }
                        $('.lose-team').css({display:'block'});
                        $('.lose-team .title .color').text(data.data.unbund_list.length);
                        $('.lose-team .newfriend-text').html(ounbund_list);
                    }else{
                        $('.lose-team').css({display:'none'});
                    }
                    var omanager_list='';
                    for(var i = 0 ; i<data.data.manager_list.length ; i++){
                        var osex = data.data.manager_list[i].sex == "woman" ? 'icon_girl' :'icon_boy';
                        omanager_list+='<div class="con" ><div class="fold-text" data-uid="'+data.data.manager_list[i].uid+'" friend_uid="'+data.data.manager_list[i].uid+'"><img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.manager_list[i].face+'" />'
                                    +'<div class="box"><div class="user">'+data.data.manager_list[i].nickname+'</div>'
                                    +'<div class="age"><img src="images/'+osex+'.png"><span>'+data.data.manager_list[i].age+'岁</span>'
                                    +'</div></div><a class="lia" href="javascript:">解绑</a></div></div>';
                    }
                    $('.team-list .mui-scroll .myup').html(omanager_list);
                    
                    //我的上级
                    var omyparent='';
                    if(data.data.parent){
                        var osex = data.data.unbund_list.sex== "woman" ? 'icon_girl' :'icon_boy';
                        omyparent+='<div class="con"><div class="fold-text"  data-uid="'+data.data.parent.uid+'" data-id="'+data.data.parent.uid+'">'
                                +' <img onerror="imgExists(this)" src="'+DOMAIN_NAME+data.data.parent.face+'"/><div class="box"><div class="user">'+data.data.parent.nickname+'</div>'
                                +'<div class="age"><img src="images/'+osex+'.png"/><span>'+data.data.parent.age+'岁</span></div></div>'
                                +'<a class="lia untie" href="javascript:">申请解绑</a></div></div>'
                    }else{
                        omyparent+='<div class="con" ><div class="fold-text pt">'
                                +' <img src="images/userindex.jpg"/><div class="box"><div class="user">平台</div></div>'
                                +'<a class="lia untie" href="javascript:">申请解绑</a></div></div>'
                    }
                    $('.myprop').html(omyparent);
                } else if(data.error_code == 1001) {
                    setTimeout(function() {
                        exitlogin();
                    },1000);
                } else {
                    if(data.error_msg=='您还不是经理人'){
                        mui.alert(data.error_msg,'提示','确定',function(){
                            setTimeout(function(){
                                mui.back();
                            },350)
                        },'div')
                    }
                }
                mui('.mui-scroll-wrapper').scroll({
                    indicators: false, //是否显示滚动条 默认为true
                    deceleration: 0.003, //阻尼系数,系数越小滑动越灵敏 默认0.0006
                    bounce: true //是否启用回弹 默认true
                });
            }
        });
    }
    /**
     * 同意解绑
     * untieagree
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    $('.handlersharp').on('tap','.untieagree',function(){//同意解绑
        var oid = $(this).parents('.fold-text').attr('data-id');
        var obj =$(this).parents('.fold-text');
        var oshu = obj.parent().parent().find('.title .color').text()-1;
        var obj2 = obj.parent().parent().find('.title .color');
        var objbox = obj.parent().parent();
        mui.confirm('您确定同意解绑？','提示',['确定','取消'],function(e){
            if(e.index==0){
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/user/agreeUnbind",
                    dataType:'jsonp',
                    data: { uid: uid,token :token,id:oid,},
                    success: function(data) {
                        if(data.error_code == 0){
                            mui.toast('解绑成功！',{ duration:2000, type:'div' });
                            obj.remove();
                            obj2.text(oshu);
                            if(oshu==0){
                                objbox.css({display:'none'});
                            }
                            lodemsg();
                        }else if(data.error_code == 1001){
                            setTimeout(function(){
                                exitlogin();
                            },1000);
                        }else{
                        }
                    }
                });
            }
        },'div')
            
    })
    /**
     * 忽略解绑
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    $('.handlersharp').on('tap','.disagree',function(){
        var oid = $(this).parents('.fold-text').attr('data-id');
        var obj =$(this).parents('.fold-text');
        var oshu = obj.parent().parent().find('.title .color').text()-1;
        var obj2 = obj.parent().parent().find('.title .color');
        var objbox = obj.parent().parent();
        var osrc, odata;
        if(objbox.hasClass('date-unbund-list')||objbox.hasClass('lose-team')){
            osrc ="/index.php/api/user/refuseUnbind";
        }else{
            //拒绝
            osrc = '/index.php/api/Users_verify/refuseVerifyApply';
        }
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + osrc,
            dataType:'jsonp',
            data: { uid: uid,token :token,id:oid,},
            success: function(data) {
                if(data.error_code == 0){
                    obj.remove();
                    obj2.text(oshu);
                    if(oshu==0){
                        objbox.css({display:'none'});
                    }
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }
        });
    })
    /**
     * 解绑
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    $('.handlersharp').on('tap','.lia',function(){
        var friend_uid = $(this).parents('.con').attr('friend_uid');
        if(!friend_uid){
            friend_uid = $(this).parents('.fold-text').attr('friend_uid');
        }
        var obj =$(this).parents('.con');
        if(!$(this).hasClass('untie')){
            if($(this).parents('.mui-scroll-wrapper').hasClass('users_list')){
                mui.confirm('您确定解绑？','提示',['确定','取消'],function(e){
                    if(e.index==0){
                        $.ajax({
                            type: "post",
                            url: DOMAIN_NAME + "/index.php/api/user/dateUnbind",
                            dataType:'jsonp',
                            data: { uid: uid,token :token,friend_uid:friend_uid,},
                            success: function(data) {
                                if(data.error_code == 0){
                                    mui.toast('解绑成功！',{ duration:2000, type:'div' });
                                    obj.remove();
                                }else if(data.error_code == 1001){
                                    setTimeout(function(){
                                        exitlogin();
                                    },1000);
                                }else{
                                }
                            }
                        });
                    }
                },'div')
            }else{
                mui.confirm('您确定解绑？','提示',['确定','取消'],function(e){
                    if(e.index==0){
                        $.ajax({
                            type: "post",
                            url: DOMAIN_NAME + "/index.php/api/user/unbind",
                            dataType:'jsonp',
                            data: { uid: uid,token :token,friend_uid:friend_uid,},
                            success: function(data) {
                                if(data.error_code == 0){
                                    mui.toast('解绑成功！',{ duration:2000, type:'div' });
                                    obj.remove();
                                }else if(data.error_code == 1001){
                                    setTimeout(function(){
                                        exitlogin();
                                    },1000);
                                }else{
                                }
                            }
                        });
                    }
                },'div')
            }
        }
        
    })
    
    //向上级解绑
    function updelet(submit_to,reason){
        $('.table-view').css({display:'none'});
        $('.popover').css({display:'none'});
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/User/managerUnbindApply",
            dataType:'jsonp',
            data: { uid: uid,token :token,submit_to:submit_to,reason:reason,},
            success: function(data) {
                if(data.error_code == 0){
                    mui.toast('已申请！')
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg)
                }
            }
        });
    }
    /**
     * 
     * mui框架时间样式
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */